/**
 * Wihidum - Efficient Broker Hierarchy for Publish Subscribe Middleware
 *
 * License      :   Apache License 2.0
 * Home Page    :   http://code.google.com/p/wihidum/
 * Mailing List :   wihidum@googlegroups.com
 */

package lk.mrt.cse.wihidum.client;

/**
 *Thisclass can be used by a subscription client or a EventSink service to form a standardized unsubscription message
 * and send it to the relevant end point.
 */

import lk.mrt.cse.wihidum.util.*;
import org.apache.axiom.om.*;
import org.apache.axiom.soap.*;
import org.apache.axis2.client.*;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.wsdl.WSDLConstants;
import org.apache.log4j.Logger;


public class UnsubscriptionClient {
    private static final Logger lgger = Logger.getLogger(UnsubscriptionClient.class);
    private UnsubscriptionClientBean unsubscriptionClientBean;

    public UnsubscriptionClient(UnsubscriptionClientBean unsubscriptionClientBean) {
        this.unsubscriptionClientBean = unsubscriptionClientBean;
    }

    public String unsubscribe() {
        OMElement responseElement = null;
        SOAPEnvelope responseEnvelope = null;
        try {
            ServiceClient serviceClient = new ServiceClient();
            OperationClient operationClient = serviceClient.createClient(ServiceClient.ANON_OUT_IN_OP);
            MessageContext outContext = new MessageContext();
            Options opts = outContext.getOptions();
            opts.setAction(WihidumConstants.Actions.Unsubscribe);
            //opts.setReplyTo(unsubscriptionClientBean.getEventSinkAddress());
            opts.setTo(unsubscriptionClientBean.getBrokerAddress());

            SOAPEnvelope unsubcriptionEnvelope = createUnsubscriptionEnelope();

            outContext.setEnvelope(unsubcriptionEnvelope);
            operationClient.addMessageContext(outContext);
            operationClient.execute(true);
            MessageContext inMsgtCtx = operationClient.getMessageContext(WSDLConstants.MESSAGE_LABEL_IN_VALUE);

            responseEnvelope = inMsgtCtx.getEnvelope();


        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
        if (responseEnvelope.getBody().getFirstElement() != null) {
            return "Subscription Manager deleted subscription successfully";
        } else
            return "Error occured in deleting subscription";

    }

    private SOAPEnvelope createUnsubscriptionEnelope() {
        SOAPFactory factory = OMAbstractFactory.getSOAP12Factory();
        SOAPEnvelope unsubscriptionEnvelope = factory.getDefaultEnvelope();

        OMNamespace wseventingNS = factory.createOMNamespace(WSEventingConstants.EVENTING_NAMESPACE, WSEventingConstants.EVENTING_PREFIX);
        unsubscriptionEnvelope.declareNamespace(wseventingNS);
        OMNamespace wsaNS = factory.createOMNamespace(WSEventingConstants.ADDRESSING_NAMESPACE, WSEventingConstants.ADDRESSING_PREFIX);
        unsubscriptionEnvelope.declareNamespace(wsaNS);
        OMNamespace wihidumNS = factory.createOMNamespace(WSEventingConstants.WIHIDUM_NAMESPACE, WSEventingConstants.WHIDUM_NAMESPACE_PREFIX);
        unsubscriptionEnvelope.declareNamespace(wihidumNS);
        SOAPHeader soapHeader = unsubscriptionEnvelope.getHeader();

        OMElement identifierElement = factory.createOMElement(WSEventingConstants.ElementNames.Identifier, wseventingNS);
        identifierElement.setText(unsubscriptionClientBean.getIdentifier());

        soapHeader.addChild(identifierElement);

        SOAPBody soapBody = unsubscriptionEnvelope.getBody();

        OMElement unsubscriptionElement = factory.createOMElement(WSEventingConstants.ElementNames.Unsubscribe, wseventingNS);
        soapBody.addChild(unsubscriptionElement);
        return unsubscriptionEnvelope;
    }

}
